package com.young.bs.vote.service.impl;

import com.young.common.exception.BaseException;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.bs.vote.model.VoteOption;
import com.young.bs.vote.service.IVoteOptionService;
import com.young.common.util.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 选项信息服务实现
* Created by admin on 2019-3-19.
*/
@Service("voteOptionService")
public class VoteOptionServiceImpl implements IVoteOptionService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(VoteOptionServiceImpl.class);

    private static final String MAPPER = "com.young.bs.vote.mapper.voteOption";//mapper的namespace

    //获取数据列表
    @Override
    public List<VoteOption> getList(VoteOption obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<VoteOption> getPage(VoteOption obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public VoteOption get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(VoteOption obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(VoteOption obj) {
        obj.setVoteId(null);//所属选票不允许修改
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(VoteOption obj) {
        if (StringUtils.isBlank(obj.getId())) {
            obj.setId(CommonUtil.getUUID());
        }
        if (StringUtils.isBlank(obj.getVoteId())) {
            throw new BaseException("投票主信息编号不允许为空");
        }
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<VoteOption> list) {
        int i = 0;
        for(VoteOption item : list){
            i += this.insert(item);
        }
        return i;
    }

}